package cn.tedu.service;

import cn.tedu.domain.User;
import cn.tedu.exception.MsgException;
import cn.tedu.mapper.UserMapper;
import cn.tedu.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void registUser(User user) {
        // 检查用户名是否已经存在
        if (userMapper.findUser(new User(0, user.getUsername(), null, null, null)).size() > 0) {
            // 存在抛出异常
            throw new MsgException("用户名已经存在");
        } else {
            // 不存在就添加用户
            userMapper.insUser(user);
        }
    }

    @Override
    public User loginUser(String username, String password) {
        User user = new User(0, username, password, null, null);
        List<User> list = userMapper.findUser(user);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public boolean userNameIsOK(String username) {
        List<User> list = userMapper.findUser(new User(0,username,null,null,null));
        return !(list.size() > 0);
    }
}
